﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace RTF_Editor_Plugin
{
    public class RTFDocument : Notepad_X.Classes.IDocument
    {
             
        private int _id;
        private Notepad_X.Components.TabPageX _tab_page;
        private string _path;
        private RichTextBox  _text_box;
        private bool _saved = false;
        private bool _available = false;

        private int _find_pos=0;
        private int _find_index = 0;
   
        private string _find_string = null;
        private bool _watcherChanged=false;
        private System.IO.FileSystemWatcher _watcher;

        public event Notepad_X.Classes.DocumentXEventHandler DocumentChanged;

        public int id { get { return _id; } }
        public Notepad_X.Components.TabPageX tab_page { get { return _tab_page; } }
        public string path { get { return _path; } }

        public bool saved { get { return _saved; } }
        public bool available { get { return _available; } }

        public System.IO.FileSystemWatcher watcher { get { return _watcher; } set { _watcher = value; } }
        public bool watcherChanged { get{return _watcherChanged;} set{_watcherChanged=value;} }
        public System.Drawing.Printing.PrintDocument printDocument { get { return null; } }
        public object text_box { get { return _text_box; } }

        public System.Drawing.Font font { get { return _text_box.Font; } set { _text_box.Font = value; } }

        public int find_pos { get { return _find_pos; } set { _find_pos = value; } }
        public int find_index { get { return _find_index; } set { _find_index = value; } }
        public string find_string { get { return _find_string; } set { _find_string = value; } }

        public string text { get { return _text_box.Text; } set { _text_box.Text = value; } }

        public RTFDocument(int pId) {
            create(pId);
        }

        public void create(int pId){
            _id = pId;
            _tab_page = new Notepad_X.Components.TabPageX(pId);
            _path = null;
            _text_box = new RichTextBox();
            _tab_page.AllowDrop = true;
            _tab_page.Controls.Add(_text_box);
            _tab_page.id = _id;

            _text_box.Tag = id;
            _text_box.Dock = System.Windows.Forms.DockStyle.Fill;
            _text_box.Location = new System.Drawing.Point(0, 0);
            _text_box.AllowDrop = true;

            _saved = true;
            _available = false;
            _tab_page.ImageKey = "documentok";
            _watcher = null;
            _text_box.TextChanged += new EventHandler(_text_box_TextChanged);
            new Notepad_X.Classes.Event.DocumentEvent(id, Notepad_X.Classes.Event.DocumentEvents.New);
        }

        void _text_box_TextChanged(object sender, EventArgs e) {
            _saved = false;
            _tab_page.ImageKey = "documentchanged";
            if (DocumentChanged != null) DocumentChanged.Invoke(this, new Notepad_X.Classes.DocumentXEventArgs(_id));
        }

        void _DocumentChanged(object sender, Notepad_X.Classes.DocumentXEventArgs e) {
            _saved = false;
            _tab_page.ImageKey = "documentchanged";
            DocumentChanged -= new Notepad_X.Classes.DocumentXEventHandler(_DocumentChanged); 
        }

        public void clear(){
            _available = true;
            _text_box.Dispose();
            _text_box = null;
            _tab_page.Dispose();
            _tab_page = null;
            _path = null;
            _id = -1;
            _saved = false;
            DocumentChanged = null;
        }

        public void save(string pPath){
            _saved = true;
            _path = pPath;

            _text_box.SaveFile(_path);

            _tab_page.ImageKey = "documentok";
            DocumentChanged += new Notepad_X.Classes.DocumentXEventHandler(_DocumentChanged);
            new Notepad_X.Classes.Event.DocumentEvent(id, Notepad_X.Classes.Event.DocumentEvents.Save);
        }
        public void open(string pPath){
            _saved = true;
            _path = pPath;

            _text_box.LoadFile(_path);

           _tab_page.ImageKey = "documentok";
           new Notepad_X.Classes.Event.DocumentEvent(id, Notepad_X.Classes.Event.DocumentEvents.Open);
            //highlight
        }

        public void cut(){
            _text_box.Cut();
        }
        public void copy(){
            _text_box.Copy();
        }
        public void paste(){
            _text_box.Paste();
        }
        public void selectAll(){
            _text_box.SelectAll();
        }

        public string selectedText() {
            return _text_box.SelectedText;
        }
        public int selectionIndex() {
            return _text_box.SelectionCharOffset;
        }
        public void insert(int index,string text) {
            _text_box.Text.Insert(index, text);
        }
        public void undo(){
            _text_box.Undo();
        }
        public void redo(){
            _text_box.Redo();
        }

        public void print(){

        }

        public void lineNumbers(bool value){

        }

        public void goTo(int line){
        }

        public void find(string text){

        }

        public void replace(string text, string newText) {

        }
    
    }
}
